package com.leyou.page.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.client.ItemClient;
import com.leyou.item.dto.*;
import com.leyou.page.service.GoodsPageService;
import com.leyou.page.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class GoodsPageServiceImpl implements GoodsPageService {

    @Autowired
    private ItemClient itemClient;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String SPU_KEY_PREFIX = "page:spu:id:";
    private static final String DETAIL_KEY_PREFIX = "page:detail:id:";
    private static final String SKU_KEY_PREFIX = "page:sku:id:";
    private static final String CATEGORY_KEY_PREFIX = "page:category:id:";
    private static final String BRAND_KEY_PREFIX = "page:brand:id:";
    private static final String SPEC_KEY_PREFIX = "page:spec:id:";


    @Override
    public String loadSpuData(Long spuId) {
        if (spuId == null || spuId < 0) {
            throw new LyException(404, "输入格式错误");
        }
        //1.查询spu信息
        SpuDTO spuDTO = itemClient.querySpuById(spuId);
        if (spuDTO == null || spuDTO.getId() == null) {
            throw new LyException(404, "商品不存在！");
        }
        //2.转换vo
        SpuVO spuVO = spuDTO.toEntity(SpuVO.class);
        //获取3级分类
        spuVO.setCategoryIds(spuDTO.getCategoryIds());
        //3.序列化为json字符串
        if (spuVO==null){
            throw new LyException(404, "查询为null");
        }
        String json = JsonUtils.toJson(spuVO);
        //4.写入redis  //opsForValue基础增删改查
        redisTemplate.opsForValue().set(SPU_KEY_PREFIX+spuId, json);
        return json;
    }

    @Override
    public String loadSpuDetailData(Long spuId) {
        if (spuId == null || spuId < 0) {
            throw new LyException(404, "输入格式错误");
        }
        SpuDetailDTO spuDetailDTO = itemClient.querySpuDetailById(spuId);
        if (spuDetailDTO==null){
            throw new LyException(404, "查询不到数据");
        }
        //3.序列化为json字符串
        String json = JsonUtils.toJson(spuDetailDTO);
        //4.写入redis  //opsForValue基础增删改查
        redisTemplate.opsForValue().set(DETAIL_KEY_PREFIX+spuId, json);
        return json;
    }

    @Override
    public String loadSkuListData(Long spuId) {
        if (spuId == null || spuId < 0) {
            throw new LyException(404, "输入格式错误");
        }
        //1.查询skuList信息
        List<SkuDTO> skuDTOS = itemClient.querySkuBySpuId(spuId);
        if (CollectionUtils.isEmpty(skuDTOS)) {
            throw new LyException(404, "查询为null");
        }
        String json = JsonUtils.toJson(skuDTOS);
        //这里要加上具体id
        redisTemplate.opsForValue().set(SKU_KEY_PREFIX + spuId, json);
        return json;
    }

    @Override
    public String loadCategoriesData(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)){
            throw new LyException(404,"ids为null");
        }
        //1.查询信息
        List<CategoryDTO> categoryDTOS = itemClient.queryCategoryByIds(ids);
        if (CollectionUtils.isEmpty(categoryDTOS)){
            throw  new LyException(404,"查询数据为null");
        }
        //2.转换vo
        List<CategoryVO> categoryVOList = categoryDTOS.stream()
                .map(categoryDTO -> categoryDTO.toEntity(CategoryVO.class))
                .collect(Collectors.toList());

        //3.序列化为json字符串
        String json = JsonUtils.toJson(categoryVOList);
        //4.写入redis  //opsForValue基础增删改查
        redisTemplate.opsForValue().set(CATEGORY_KEY_PREFIX + ids.get(2),json);
        return json;
    }

    @Override
    public String loadBrandData(Long brandId) {
        if (brandId == null || brandId < 0) {
            throw new LyException(404, "输入格式错误");
        }
        //1.查询信息
        BrandDTO brandDTO = itemClient.queryBrandById(brandId);
        if (brandDTO==null){
            throw  new LyException(404,"查询不到数据");
        }
        //2.转换vo
        BrandVO brandVO = brandDTO.toEntity(BrandVO.class);
        //3.序列化为json字符串
        String json = JsonUtils.toJson(brandVO);
        //4.写入redis  //opsForValue基础增删改查
        redisTemplate.opsForValue().set(BRAND_KEY_PREFIX+brandId,json);
        return json;
    }

    @Override
    public String loadSpecData(Long categoryId) {
        if (categoryId == null || categoryId < 0) {
            throw new LyException(404, "输入格式错误");
        }
        //1.查询信息
        List<SpecGroupDTO> specGroupDTOList = itemClient.querySpecList(categoryId);

        if (CollectionUtils.isEmpty(specGroupDTOList)){
            throw  new LyException(404,"查询不到数据");
        }
        List<SpecGroupVO> groupVOList = new ArrayList<>(specGroupDTOList.size());
        for (SpecGroupDTO specGroup : specGroupDTOList) {
            SpecGroupVO groupVO = new SpecGroupVO();
            groupVOList.add(groupVO);
            groupVO.setName(specGroup.getName());
            List<SpecParamVO> params = specGroup.getParams()
                    .stream()
                    .map(paramDTO -> paramDTO.toEntity(SpecParamVO.class))
                    .collect(Collectors.toList());
            groupVO.setParams(params);
        }
        // 3.序列化为json
        String json = JsonUtils.toJson(groupVOList);
        // 4.写入redis,加id
        redisTemplate.opsForValue().set(SPEC_KEY_PREFIX + categoryId, json);
        // 5.返回
        return json;
    }

    @Override
    public Boolean deleteSku(Long spuId){
        return redisTemplate.delete(SKU_KEY_PREFIX + spuId);
    }
}
